package com.android.assignment.taskreminder;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.util.Log;

public class ReminderService extends WakeReminderIntentService {

	public ReminderService() {
		super("ReminderService");
	}

	// @SuppressWarnings("deprecation")
	@Override
	void doReminderWork(Intent intent) {
		Log.d("ReminderService", "Doing work.");
		Long rowId = intent.getExtras().getLong(
				TaskReminderProvider.COLUMN_ROWID);

		NotificationManager mgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

		Intent notificationIntent = new Intent(this,
				ActionAfterRemindActivity.class);
		notificationIntent.putExtra(TaskReminderProvider.COLUMN_ROWID, rowId);

		PendingIntent pi = PendingIntent.getActivity(this, 0,
				notificationIntent, PendingIntent.FLAG_ONE_SHOT);

		/*
		 * Notification note = new Notification(
		 * R.drawable.notification_warning,
		 * getString(R.string.notify_new_task_message),
		 * System.currentTimeMillis());
		 */
		Notification note = new Notification.Builder(this)
				.setSmallIcon(android.R.drawable.stat_sys_warning)
				.setTicker(getString(R.string.notify_new_task_message))
				.setWhen(System.currentTimeMillis())
				.setContentTitle(getString(R.string.notify_new_task_title))
				.setContentText(getString(R.string.notify_new_task_message))
				.setContentIntent(pi).build();
		/*
		 * note.setLatestEventInfo(this,
		 * getString(R.string.notify_new_task_title),
		 * getString(R.string.notify_new_task_message), pi);
		 */
		//note.defaults |= Notification.DEFAULT_SOUND;
		SharedPreferences preferences = PreferenceManager
				.getDefaultSharedPreferences(getBaseContext());
		String ringtone = preferences.getString(
				getString(R.string.pref_default_ringtone_key), null);
		//Log.d("ringtone", ringtone);
		if(ringtone != null)
			note.sound = Uri.parse(ringtone);
		else
			note.defaults |= Notification.DEFAULT_SOUND;
		note.flags |= Notification.FLAG_AUTO_CANCEL;
		note.vibrate = new long[] { 100, 200, 100, 500 };
		int id = (int) ((long) rowId);
		mgr.notify(id, note);

	}
}
